export const userState = defineStore('user', {
    state: () => {
        return {
            token:"",
            userId:"",
            systemId:"",
            sysname:"",
            syscode:"",
            account:"",
            username:"",
            avatar:"",
            uuid:"",
            roles:[],
            menus:[],
            systemList:[],
            permissions:[]
        }
    },
    getters: {
        // 定义的时候是方法，使用的是属性
        isLogin(){
            return this.userId
        },
        // 把所有的角色对应的菜单全部变成一级
        menuList(){
            var menu = {}
            this.menus.forEach(c=>{
                menu[c.path] = c.id
                if(c.children && c.children.length > 0){
                    c.children.forEach(cc=>{
                        menu[cc.path] = cc.id
                    })
                }
            })
            return menu
        },
        // 获取每个角色的第一个访问的路由
        indexPath(){
            if(this.menus && this.menus.length > 0) {
                var menuObj = this.menus[0]
                var path = ""
                if (menuObj?.children && menuObj?.children.length > 0) {
                    path = menuObj?.children[0].path;
                } else {
                    path = menuObj?.path
                }
                return path
            }else{
                return "/admin"
            }
        }
    },
    actions: {
        changeSystem(system,token){
            this.systemId = system.id
            this.sysname = system.sysname
            this.syscode = system.syscode
            this.token = token
        },
        setMenuApis(menuApis) {
            if(menuApis.permissions && menuApis.permissions.length > 0) {
                this.permissions = menuApis.permissions.map(c=>c.code);
            }else{
                this.permissions = []
            }
            if(menuApis.menus && menuApis.menus.length > 0) {
                this.menus = menuApis.menus.map(c=>{
                    if(!c.children){
                        c.children = []
                    }
                    return c;
                });
            }else{
                this.menus = []
            }
            return Promise.resolve()
        },
        setUserInfo(loginUser) {
            this.userId = loginUser.id;
            this.account = loginUser.account;
            this.username = loginUser.username;
            this.systemId = loginUser.systemId;
            this.syscode = loginUser.syscode;
            this.sysname = loginUser.sysname;
            this.avatar = loginUser.avatar;
            this.token = loginUser.token;
            this.uuid = loginUser.uuid;
            if(loginUser.systemList && loginUser.systemList.length > 0) {
                this.systemList = loginUser.systemList.map((c,index)=>{
                    return {...c,active:index==0};
                });
            }
            if(loginUser.roles && loginUser.roles.length > 0) {
                this.roles = loginUser.roles.map((c,index)=>{
                    return {id:c.id,name:c.roleName,active:index==0};
                });
            }
            if(loginUser.permissions && loginUser.permissions.length > 0) {
                this.permissions = loginUser.permissions.map(c=>c.code);
            }
            if(loginUser.menus && loginUser.menus.length > 0) {
                this.menus = loginUser.menus.map(c=>{
                    if(!c.children){
                        c.children = []
                    }
                    return c;
                });
            }
        },
        clearUserInfo(){
            if(process.client){
                this.$reset();
                localStorage.removeItem("kva-cc-userinfo");
            }
        }
    },
    //persist定义要做判断，因为localStorage是客户端参数，所以需要加process.client
    persist: process.client && {
        storage: localStorage, // sessionStorage/localStorage
        key: 'kva-cc-userinfo', // storage的key
    }
})
